package com.yupi.springbootinit.config;

/**
 * @Author: LiangXinRui
 * @Date: 2023/08/03/15:06
 * @Description:
 */

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yupi.springbootinit.bizmq.BiMessageProducer;
import com.yupi.springbootinit.bizmq.BiMqConstant;
import com.yupi.springbootinit.service.ChartService;
import com.yupi.springbootinit.utils.RabbitMqUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Component
@Slf4j
public class DemoJobHandler {

    @Resource
    private BiMessageProducer biMessageProducer;

    @Resource
    private ChartService chartService;

    /**
     * 1、简单任务示例（Bean模式）
     */
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        System.out.println("hello,XXL-JOB!!!");
    }

    @XxlJob("retryJobHandler")
    public void retryJobHandler() throws Exception {
        log.info("retryJobHandler......");
        List<Long> list = chartService.getChartListWithRetryNumber();
        Channel channel = RabbitMqUtils.getChannel();
        list.forEach(value -> {
            try {
                channel.basicPublish("", BiMqConstant.BI_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, String.valueOf(value).getBytes());
            } catch (IOException e) {
                // 处理异常，记录日志或执行其他补偿措施
                log.error("Error processing XXL-job letter message. ");
                //更新retry字段,retry++
                chartService.retryIncrease(value);
            }
        });
    }
}

